package com.apps.realtyportal;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.apps.essentials.ConsumeWebService;
import com.jason.apps.buyer.Buyer;
import com.jason.apps.buyer.RegisterBuyer;
import com.jason.apps.dashboard.DashBoard;
import com.jason.apps.fromdashboard.Agent;

public class LoginActivity extends SherlockFragmentActivity {

	private EditText email;
	private EditText pass;
	private Button register;
	private TextView tusername;
	private TextView tpassword;
	boolean islogin;
	ConsumeWebService login = new ConsumeWebService();
	SherlockFragmentActivity fa;
	Context context;
	FragmentManager fm;
	FragmentTransaction ft;
	public static int id = -1;
	public static String PersonType;
	private ActionBar actionbar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setLogo(R.drawable.logo3);
		actionbar.setDisplayShowTitleEnabled(false);
		email = (EditText) this.findViewById(R.id.email_add);
		pass = (EditText) this.findViewById(R.id.pass);
		register = (Button) this.findViewById(R.id.btnLogin);
		tusername = (TextView) this.findViewById(R.id.txtforusername);
		tpassword = (TextView) this.findViewById(R.id.txtforpassword);

		fm = getSupportFragmentManager();
		ft = fm.beginTransaction();

		// ActionBar actionBar = getSupportActionBar();

		// actionBar.setDisplayShowTitleEnabled(false);
		// actionBar.setDisplayHomeAsUpEnabled(true);
		TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

		// Listening to register new account link
		registerScreen.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// Switching to Register screen
				startActivity(new Intent(getApplicationContext(),
						RegisterBuyer.class));
				finish();
			}
		});
	}

	public void Login(View v) {

		if ((!email.getText().toString().trim().equals(""))
				&& (!pass.getText().toString().trim().equals(""))) {
			new Login().execute();
		}

		else if ((email.getText().toString().trim().equals(""))
				&& (pass.getText().toString().trim().equals(""))) {
			tusername.setText("Username Required");
			tusername.setTextColor(Color.RED);
			tpassword.setText("Password Required");
			tpassword.setTextColor(Color.RED);
			Toast.makeText(getApplicationContext(),
					"Enter Username and Password", Toast.LENGTH_LONG).show();
		} else if ((!email.getText().toString().trim().equals(""))
				&& (pass.getText().toString().trim().equals(""))) {
			tusername.setText("Username");
			tusername.setTextColor(Color.BLACK);
			tpassword.setText("Password Required");
			tpassword.setTextColor(Color.RED);
		} else if ((email.getText().toString().trim().equals(""))
				&& (!pass.getText().toString().trim().equals(""))) {
			tusername.setText("Username Required");
			tusername.setTextColor(Color.RED);
			tpassword.setText("Password");
			tpassword.setTextColor(Color.BLACK);
		}
	}

	/**
	 * This callback method will be invoked when the user selects up navigation
	 * icon from the action bar
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, HomeActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean determineLogin() {
		return islogin;
	}

	public void setIsLogIn(Boolean logIn) {
		this.islogin = logIn;
	}

	public boolean getIsLogIn() {
		return this.islogin;
	}

	public class Login extends AsyncTask<Void, Void, Void> {
		SherlockFragmentActivity activity;
		public ProgressDialog dialog = new ProgressDialog(LoginActivity.this);
		boolean results = false;

		protected void onPreExecute() {

			this.dialog.setMessage("Loading...");
			this.dialog.show();

		}

		protected Void doInBackground(final Void... unused) {
			String username = email.getText().toString().trim();
			String password = pass.getText().toString().trim();

			try {
				results = login.Login(username, password);
				PersonType = login.PersonType(username);

				if (results == true) {

					id = login.GetId(username);
					HomeActivity.islogin = true;
					if (login.PersonType(username).equals("Agent")) {

						Intent i = new Intent(getApplicationContext(),
								Agent.class);
						startActivity(i);
					} else if (login.PersonType(username).equals(
							"Administrator")) {

						Intent i = new Intent(getApplicationContext(),
								DashBoard.class);
						startActivity(i);
					} else {
						Intent i = new Intent(getApplicationContext(),
								Buyer.class);
						startActivity(i);
					}

				}
			} catch (Exception e) {
				Log.e("", "", e);
			}

			return null;
		}

		protected void onPostExecute(Void result) {
			if (results) {
				Toast.makeText(getApplicationContext(), "Success",
						Toast.LENGTH_SHORT).show();
				finish();
			} else {
				Toast.makeText(getApplicationContext(), "Not Match",
						Toast.LENGTH_SHORT).show();

			}
			if (this.dialog != null) {
				this.dialog.dismiss();
			}

		}
	}

	@Override
	public void onBackPressed() {
		Toast.makeText(getApplicationContext(), "Please use the up button",
				Toast.LENGTH_LONG).show();
	}
}